package com.ayuer.chapter08;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;

public class InsertionSortDemo {
    public static void main(String[] args) {
        int[] arr = {5, 4, 3, 2, 1};
        insertion(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void insertion(int[] arr) {
        //low无序区首个元素
        for (int low = 1; low < arr.length; low++) {
            //
            int t = arr[low];
            int i = low - 1;//有序区域边界
            while (i >= 0 && t < arr[i]) {
                arr[i + 1] = arr[i];
                i--;
            }

            //i是不停改变，如果不变说明不需要移动
            arr[i + 1] = t;
        }

    }
}
